package com.google.donutrush;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Entities.RTentry;
import Entities.User;
import Services.FBMailman;
import Services.Mailman;

import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

@SuppressWarnings("serial")
public class DonutRushServlet extends HttpServlet {
	
	public void Get(HttpServletRequest request, HttpServletResponse response)
	throws IOException {
		doPost(request, response);
	
	}
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException {
		
		//Prepare the json object we create from the request body
		JSONObject jsonObject = null;
		//The object comes in the body as the key in the first element of the ParameterMap (im not sure why)
		//I only converted it to array because I like it better
		Object[] m = request.getParameterMap().keySet().toArray();
		//Create the json from the object in m[0]
		try {
			jsonObject = new JSONObject(m[0].toString());
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		
		//getting all filled params in the registration form of the json object
		String name = "";
		String email = "";
		String password = "";
		int score = 0;
		int loggedIn = 0;
		int SignLevel = 0;
		int facebook = 0;
		String facebookID = "";
		
		try {
			name = (String) jsonObject.get("name");
			email = (String) jsonObject.get("email");
			password = (String) jsonObject.get("password");
			score = (Integer) jsonObject.get("score");
			loggedIn = (Integer) jsonObject.get("loggedIn");
			SignLevel = (Integer) jsonObject.get("SignLevel");
			facebook = (Integer) jsonObject.get("facebook");
			if(facebook == 1){
				facebookID = (String) jsonObject.get("facebookID");
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}

		
		//creating a user from the filled params
		
		User newuser;
		if(facebook == 0){
			newuser = new User(name, email, password, false);
		}
		else{
			newuser = new User(name, email, password, facebookID, true); 
			
			FBMailman.postScoreToTimeLine(facebookID, score);
			
		}
		newuser.setBestScore(score);//inserting to the user score achieved
		request.setAttribute("newUser", newuser);
		
		//creating a Record-table entry
		if(SignLevel != 0){
			if(facebook ==0){
			RTentry newRTentry = new RTentry(name, score);
			request.setAttribute("newRTentry", newRTentry);
			}
			else{//this is a facebook user
				RTentry newRTentry = new RTentry(name, facebookID, score);
				request.setAttribute("newRTentry", newRTentry);
			}
		}
		
		//is user logged 
		request.setAttribute("loggedIn", loggedIn);
		request.setAttribute("SignLevel", SignLevel);
		request.setAttribute("facebookUser", facebook);
		
		
		
	
		//move on to servlet that signs up user and score
		RequestDispatcher rd = request.getRequestDispatcher("Register.do");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
}


	
}
